#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/6/2  11:17
# @Author: 余浪人
# @email: yulangren520@gmail.com
from spider import save_img
from spider.crawler import queue, download_get
import datetime, re
from lxml import etree


def xpath_analysis_detail():
    data = queue.get()
    url = data.get('detail_url')
    if url:
        html = etree.HTML(download_get(url).text)
        title = ''.join(html.xpath('//h1[@class="main-tit2"]//a/text()'))
        create_time = ''.join(html.xpath('//p[@class="icogroup lh180 loginfo"]/span[2]/text()'))
        create_time = re.findall('(\d{2,4}([\.\-/|年月\s]{1,2}\d{1,2}){1,2}日?)', create_time)
        create_time = create_time[0][0] if len(create_time) else datetime.datetime.now().strftime('%Y-%m-%d')  # 发布时间
        content = '<br/>'.join(html.xpath('//div[@class="art-content pt10 f16 lh200"]/p/span/text()'))  # 内容


def re_analysis_detail(url):
    response = download_get(url=url).text
    title = re.search('<li class="last">([\s\S]*?)</li>', response).group(1)  # 标题
    create_time = re.search('时间：([\s\S]*?)</span><', response).group(1)  # 创建时间
    content_obj = re.search('<div class="art-content pt10 f16 lh200">([\s\S]*?)<!--内容结束-->', response)  # 内容
    if content_obj:
        content = content_obj.group(1)
        # content = re.sub(r"\s+", "", content) # 去除空白字符
        content = re.sub(r"</?span(.+?)?>", "", content)  # 过滤指定字符串
        content = re.sub(r"</?aid(.+?)?>", "", content)  # 过滤指定字符串
    else:
        content = ''
    content = save_img(content)  # 保存图片



if __name__ == '__main__':
    re_analysis_detail('http://www.zzlzl.com/post-3434.html')
